/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.huikecloud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.modules.huikecloud.entity.Host;
import org.springblade.modules.huikecloud.entity.Port;
import org.springblade.modules.huikecloud.excel.HostExcel;
import org.springblade.modules.huikecloud.mapper.HostMapper;
import org.springblade.modules.huikecloud.service.IHostService;
import org.springblade.modules.huikecloud.vo.HostVO;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 主机表 服务实现类
 *
 * @author Blade
 * @since 2021-07-21
 */
@Service
public class HostServiceImpl extends BaseServiceImpl<HostMapper, Host> implements IHostService {

	@Override
	public IPage<HostVO> selectHostPage(IPage<HostVO> page, HostVO host) {
		return page.setRecords(baseMapper.selectHostPage(page, host));
	}

	@Override
	public boolean submit(Host host) {
		QueryWrapper<Host> queryWrapper=new QueryWrapper<>();
		Host one = this.getOne(queryWrapper.lambda().eq(Host::getHostIp, host.getHostIp()));
		if (one!=null){
			host.setId(one.getId());
		}
		return saveOrUpdate(host);
	}

	@Override
	public void importHost(List<HostExcel> data) {
		data.forEach(hostExcel -> {
			Host host = Objects.requireNonNull(BeanUtil.copy(hostExcel, Host.class));
			this.submit(host);
		});
	}

	@Override
	public R mapping(Port port, Integer change) {
//       验证服务器信息
		QueryWrapper<Host> hostQueryWrapper = new QueryWrapper<>();
		Host host = getOne(hostQueryWrapper.lambda().eq(Host::getHostIp, port.getIp()));
		if (host == null || StringUtils.checkValNull(host.getHostIp())) {
			return R.fail("IP不存在");
		} else if (host.getTcpSize() >= host.getTcpLimit()) {
			return R.fail("此IP的TCP映射已达上限，请回收闲置映射后重试");
		} else if (host.getWebSize() >= host.getWebLimit()) {
			return R.fail("此IP的WEB映射已达上限，请回收闲置映射后重试");
		}


		if ("tcp".equals(port.getMappingType())){
			host.setTcpSize(Math.max(host.getTcpSize() + change, 0));
		}else {
			host.setWebSize(Math.max(host.getWebSize()+change,0));
		}
		return R.status(saveOrUpdate(host));
	}

}
